package GUI;


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import org.jdesktop.application.Application;

import javax.swing.SwingUtilities;


import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RM extends javax.swing.JFrame {
	private JPanel zero;
	private JPanel text;


	private JPanel title;
	private JPanel jPanel3;
	private JPanel jPanel1;
	private JLabel jLabel1;
	private static JButton SearchButton;
	private static JButton MyMenuButton;
	private static JButton RMButton;
	private JPanel jPanel2;
	private JPanel heading;
	private static String UserAge;
	private static String UserSex;    //1 for man , 0 for woman
	private JTable jTable1;
	
	public String food;
	public int price;
	public int calories;
	public int proteins;
	public int carbons;
	public int fats;
	
	
	
	//  This is a restaurant food mean started
	
	private static JScrollPane scrollpane;
	 private static JButton BackRM;
	private static String foodname[][] = new String[30][1];
	private static int   foodprice[] = new int[30];
	private static int   calorie[]   = new int[30];
	private static int   protein[]   = new int[30];
	private static int   carbon[]    = new int[30];
	private static int   fat[]       = new int[30];
	private static String location[] = new String[30];
	private static String warning[]  = new String[30];
	
	public static LinkedList<String> name = new LinkedList<String>();
	static JList list = new JList();
	
	
	
	// Data base Setting
	 private static Connection con = null; //Database objects 
	 private JLabel warningLabel;
	 private JLabel locationLabel;
	private static JLabel caorlie_label;
	 private static JLabel fat_label;
	 private static JLabel carbon_label;
	 private static JLabel protein_label;
	 private static JButton AddMyMenu;
	 private static JLabel ItemPrice;
	 private static JLabel label;
	 private static Statement stat = null; 
	 private static ResultSet rs = null; 
	 private static PreparedStatement pst = null; 	  
	 public static String selectLocation ;
	 public String insertSQL = "insert into mydb.mymenu values (?,?,?,?,?,?) ";
	 
	  public void insertTable( String foodname, int foodprice,int calories, int proteins,int carbons, int fat) 
	  { 
	    try 
	    { 
	      pst = con.prepareStatement(insertSQL); 
	      pst.setString(1, foodname);
	      pst.setInt(2, foodprice);
	      pst.setInt(3, calories);
	      pst.setInt(4, proteins);
	      pst.setInt(5, carbons);
	      pst.setInt(6, fat);
	      pst.executeUpdate(); 
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("InsertDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    } 
	  } 
	 
	 public void location(String Location,boolean pass)
	 {
		 if(pass == true)
		 {
			selectLocation = Location;
			RM inst = new RM();
			SelectTable(selectLocation);
			inst.setLocationRelativeTo(null);
			inst.setVisible(true);

		 }
	 }
	 	 
	 public static void begin()
	 {
	    try { 
		      Class.forName("com.mysql.jdbc.Driver"); 
		      con = DriverManager.getConnection( 
		      "jdbc:mysql://localhost/mydb", 
		      "root",""); 

		    } 
		    catch(ClassNotFoundException e) 
		    { 

		      System.out.println("DriverClassNotFound :"+e.toString()); 
		    }
		    catch(SQLException x) { 
		      System.out.println("Exception :"+x.toString()); 
		    } 
	 }
	 
	 public static void SelectTable(String select) 
	 { 	
		 list.setListData(new String[0]);
		 begin();
	    try 
	    { 
	      stat = con.createStatement(); 
	      rs = stat.executeQuery(select); 
	      int counting = 0;
	      
	      while(rs.next()) 
	      { 
	    	  warning[counting]  = rs.getString("warning");
	    	  location[counting] = rs.getString("offerBy");
	    	  String present = rs.getString("name");
	    	  name.add(present);    
	    	  foodname[counting][0] = present;
	    	  foodprice[counting] = rs.getInt("price");
	    	  calorie[counting] = rs.getInt("calories");
	    	  protein[counting] = rs.getInt("protein");
	    	  carbon[counting] = rs.getInt("carbohydrate");
	    	  fat[counting] = rs.getInt("fat");
	    	  counting++;
	      } 
	      list.setListData(new String[0]);
	      list.setListData(name.toArray(new Object[0]));
	      //list.setListData(new String[0]);
	     // list.remove(0);
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    } 
	  } 
	 
	  private static void Close() 
	  { 
	    try 
	    { 
	      if(rs!=null) 
	      { 
	        rs.close(); 
	        rs = null; 
	      } 
	      if(stat!=null) 
	      { 
	        stat.close(); 
	        stat = null; 
	      } 
	      if(pst!=null) 
	      { 
	        pst.close(); 
	        pst = null; 
	      } 
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("Close Exception :" + e.toString()); 
	    } 
	  } 
	
	  // End of Database setting

	
	
	

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				/*RM inst = new RM();
				SelectTable(selectLocation);
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);*/
			}
		});
	}
	
	public RM(String sql)
	{
		super();
		selectLocation = sql;
		initGUI();
	}
	
	public RM()
	{
		super();
		initGUI();
	}
	

	


	class RestaurantMenu implements ActionListener{    
		public void actionPerformed(ActionEvent e){
			MainPage inst = new MainPage();
			inst.setVisible(true);
			dispose();
			}}
	 class MyMenu implements ActionListener{    
		public void actionPerformed(ActionEvent e){
			myMenu frame = new myMenu();
			frame.setVisible(true);
			dispose();
			}}
	class Search implements ActionListener{    
		public void actionPerformed(ActionEvent e){
			search frame = new search();
			frame.setVisible(true);
			dispose();
			}}
    
	class ActionBackRM implements ActionListener{    
		public void actionPerformed(ActionEvent e){		
				MainPage inst = new MainPage();
				inst.setVisible(true);
				dispose();


			}}
	
	class ActionMyMenu implements ActionListener{    
		public void actionPerformed(ActionEvent e){		

			
			insertTable(food,price,calories,proteins,carbons,fats);

			}}
	
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.0, 0.1};
			thisLayout.rowHeights = new int[] {54, 7};
			thisLayout.columnWeights = new double[] {0.0, 0.1};
			thisLayout.columnWidths = new int[] {113, 7};
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				zero = new JPanel();
				getContentPane().add(zero, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				heading = new JPanel();
				getContentPane().add(heading, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				heading.setName("heading");
				{
					jLabel1 = new JLabel();
					heading.add(jLabel1);
					jLabel1.setName("jLabel1");
					jLabel1.setPreferredSize(new java.awt.Dimension(398, 47));
				}
			}
			{
				jPanel2 = new JPanel();
				GridBagLayout jPanel2Layout = new GridBagLayout();
				jPanel2Layout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0};
				jPanel2Layout.rowHeights = new int[] {40, 40, 40, 40};
				jPanel2Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
				jPanel2Layout.columnWidths = new int[] { 7};
				jPanel2.setLayout(jPanel2Layout);
				getContentPane().add(jPanel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				jPanel2.setName("jPanel2");
				{
					RMButton = new JButton();
					jPanel2.add(RMButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					RMButton.setName("jButton5");
					RMButton.addActionListener(new RestaurantMenu());   
				}
				{
					SearchButton = new JButton();
					jPanel2.add(SearchButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					SearchButton.setName("jButton7");
					SearchButton.setLocale(new java.util.Locale("da"));
					SearchButton.addActionListener(new Search());   
				}
				{
					MyMenuButton = new JButton();
					jPanel2.add(MyMenuButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					MyMenuButton.setName("jButton6");
					MyMenuButton.addActionListener(new MyMenu());   
				}
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				jPanel1.setName("jPanel1");
				{
					JButton jButton8 = new JButton();
					jPanel1.add(jButton8);
					jButton8.setName("jButton8");
					jButton8.setPreferredSize(new java.awt.Dimension(100, 40));
					try {
					    Image img = ImageIO.read(getClass().getResource("2.jpg"));
					    jButton8.setIcon(new ImageIcon(img));
					  } catch (IOException ex) {
						  
					  }
				}

			}
			{
				jPanel3 = new JPanel();
				GridBagLayout jPanel3Layout = new GridBagLayout();
				getContentPane().add(jPanel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				jPanel3.setName("jPanel3");
				jPanel3Layout.rowWeights = new double[] {0.0, 0.1, 0.1, 0.1};
				jPanel3Layout.rowHeights = new int[] {32, 7};
				jPanel3Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
				jPanel3Layout.columnWidths = new int[] { 7};
				jPanel3.setLayout(jPanel3Layout);
				{
					title = new JPanel();
					jPanel3.add(title, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					title.setName("title");
				}
				{
					text = new JPanel();
					GridBagLayout textLayout = new GridBagLayout();
					jPanel3.add(text, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					text.setName("text");
					textLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					textLayout.rowHeights = new int[] {7, 7, 7, 7};
					textLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					textLayout.columnWidths = new int[] {7, 7, 7, 7};
					text.setLayout(textLayout);
					//    Food detail part
					
					{
						caorlie_label = new JLabel();
						text.add(caorlie_label, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						caorlie_label.setName("calorie is ");
					}
					{
						SelectTable(selectLocation);
						TableModel jTable1Model = 
								new DefaultTableModel(foodname,
										new String[] {  "Column 2" }
										);
						jTable1 = new JTable();
						jPanel3.add(jTable1);
						jTable1.setModel(jTable1Model);
						jTable1.setPreferredSize(new java.awt.Dimension(290, 156));
						jTable1.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
						jTable1.setName("jTable1");
						
						text.add(jTable1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						
					   // jTable1.setCellSelectionEnabled(true);
						jTable1.addAncestorListener(null);
					    ListSelectionModel cellSelectionModel = jTable1.getSelectionModel();
					    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

					    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
					      public void valueChanged(ListSelectionEvent e) {
					        String selectedData = null;

					        int[] selectedRow = jTable1.getSelectedRows();
					        int[] selectedColumns = jTable1.getSelectedColumns();
					        int index = 0;
					        for (int i = 0; i < selectedRow.length; i++) {
					          for (int j = 0; j < selectedColumns.length; j++) {
					            selectedData = (String) jTable1.getValueAt(selectedRow[i], selectedColumns[j]);
					            index = selectedRow[i];
					          }
					        }
							caorlie_label.setText(foodname[index][0] );
							ItemPrice.setText("$: " + foodprice[index] );
							caorlie_label.setText("calorie: " + calorie[index] );
							protein_label.setText("protein: " + protein[index] );
							carbon_label.setText("carbon: " + carbon[index] );
							fat_label.setText("fat:" + fat[index] );
							locationLabel.setText("Location: " + location[index]);
							if(!warning[index].isEmpty())
							{
								warningLabel.setText(warning[index]);
								warningLabel.setForeground(Color.red);
							}
							if(warning[index].isEmpty())
							{
								warningLabel.setText("");
								//warningLabel.setForeground(Color.red);
							}
							food = foodname[index][0];
							price = foodprice[index];
							calories = calorie[index];
							proteins = protein[index];
							carbons = carbon[index];
							fats = fat[index];	
					      }

					    });
					    
					}
					{
						warningLabel = new JLabel();
						text.add(warningLabel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						warningLabel.setName("warningLabel");
						//warningLabel.setText("warning");
					}
					{
						locationLabel = new JLabel();
						text.add(locationLabel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						locationLabel.setName("locationLabel");
						locationLabel.setLocale(new java.util.Locale("zh"));
					}
					{
						label = new JLabel();
						text.add(label, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						label.setName("label");
						label.setText("Price is");
					}
					{
						ItemPrice = new JLabel();
						text.add(ItemPrice, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						ItemPrice.setName("ItemPrice");
						ItemPrice.setText("$");
					}
					{
						AddMyMenu = new JButton();
						text.add(AddMyMenu, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						AddMyMenu.setName("AddMyMenu");
						AddMyMenu.setText("AddMyMenu");
						AddMyMenu.addActionListener(new ActionMyMenu()); 
					}
					{
						protein_label = new JLabel();
						text.add(protein_label, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						protein_label.setName("jLabel2");
						//protein_label.setText("protein: ");
						//protein_label.setSize(45, 17);
					}
					{
						carbon_label = new JLabel();
						text.add(carbon_label, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						carbon_label.setName("jLabel3");
						//carbon_label.setText("carbon");
					}
					{
						fat_label = new JLabel();
						text.add(fat_label, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						fat_label.setName("jLabel4");
					}
					{
						BackRM = new JButton();
						text.add(BackRM, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						BackRM.setName("BackRM");
						BackRM.setText("Back");
						BackRM.addActionListener(new ActionBackRM()); 
					}
//					end of food detail part

				}

			}

			pack();
			this.setSize(650, 600);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		//list.setListData(new String[0]);
	}


}
